package com.plian.system.controller.transfer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.Tools.DateUtil;
import com.plian.system.config.AppKeySign;
import com.plian.system.dto.transfer.DataPlatSign;
import com.plian.system.entity.fr.bank.BankKey;
import com.plian.system.entity.fr.procurement.MaterialProcurement;
import com.plian.system.entity.sys.Org;
import com.plian.system.model.R;
import com.plian.system.model.token.PassToken;
import com.plian.system.service.fr.bank.IBankKeyService;
import com.plian.system.service.fr.procurement.IMaterialProcurementService;
import com.plian.system.service.sys.orgzation.IOrgService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO 大宗物资-输出
 * @Date: 2022/11/8 4:14 PM
 * @Version: 1.0
 **/
@RestController
@RequestMapping("api/materialTransfer")
@AllArgsConstructor
@Api(value = "大宗物资-输出", tags = "大宗物资输出")
@Slf4j
public class MaterialTransferController {

    private IBankKeyService bankKeyService;

    private IMaterialProcurementService materialProcurementService;

    private IOrgService orgService;

    /**
     * 提供时间段内的大宗物资-数据接口
     * @param dataPlatSign
     */
    @PostMapping(value = "/material-time")
    @PassToken
    @AppKeySign
    public R<List<MaterialProcurement>> getMaterialProcurement(@RequestBody DataPlatSign dataPlatSign){
        log.debug("toklen:" + dataPlatSign.getToken());
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        List<Org> allChild = orgService.getAllChild(topGroupId);
        List<String> orgIds = allChild.stream().map(Org::getId).collect(Collectors.toList());
        List<MaterialProcurement> list = new ArrayList<>();
        if (Optional.ofNullable(dataPlatSign.getEndTime()).isPresent()){
            String startTime = DateUtil.format(dataPlatSign.getStartTime(), DateUtil.PATTERN_DATE);
            String endTime = DateUtil.format(DateUtil.minusMillis(DateUtil.plusDays(dataPlatSign.getEndTime(), 1), 1), DateUtil.PATTERN_DATE);
            List<MaterialProcurement> reprotPro = materialProcurementService.list(new LambdaQueryWrapper<MaterialProcurement>().eq(MaterialProcurement::getStatus, 3).eq(MaterialProcurement::getIsDeleted, 0)
                    .eq(MaterialProcurement::getDataSource, 1).between(MaterialProcurement::getFixtureDate, startTime, endTime));

            List<MaterialProcurement> platePro = materialProcurementService.list(new LambdaQueryWrapper<MaterialProcurement>().eq(MaterialProcurement::getStatus, 3).eq(MaterialProcurement::getIsDeleted, 0)
				.eq(MaterialProcurement::getDataSource, 0).between(MaterialProcurement::getCreateTime, startTime, endTime));

            reprotPro.addAll(platePro);
            list = reprotPro;
        }else {
            list = materialProcurementService.list(new LambdaQueryWrapper<MaterialProcurement>().in(MaterialProcurement::getFillingUnitId, orgIds)
                    .eq(MaterialProcurement::getStatus,3));
        }
        return R.data(list);
    }

}
